/*
 * commons is a based project implemented
 * Copyright (C) 2024 Jasmine
 */
package com.easy.core.utils;

import java.util.Collection;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import com.easy.core.exception.GlobalErrorInfoException;
import com.easy.core.exception.ResultInfoInterface;

/**
 * 异常工具类
 *
 * @author Jasmine
 * @since 1.0.0
 */
@SuppressWarnings("unused")
public class ExceptionUtil {

    /**
     * 抛异常
     *
     * @param message 异常信息
     */
    public static void send(String message) {
        throw new GlobalErrorInfoException(message);
    }

    /**
     * 抛异常
     *
     * @param code 错误码
     * @param message 异常信息
     */
    public static void send(int code, String message) {
        throw new GlobalErrorInfoException(code, message);
    }

    /**
     * 抛异常
     *
     * @param errorInfo 异常信息
     */
    public static void send(ResultInfoInterface errorInfo) {
        throw new GlobalErrorInfoException(errorInfo);
    }

    /**
     * 如果object为null，则抛异常
     */
    public static void isNull(Object object, String errorInfo) {
        if (Objects.isNull(object)) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    public static void isNull(Object object, int code, String errorInfo) {
        if (Objects.isNull(object)) {
            throw new GlobalErrorInfoException(code, errorInfo);
        }
    }

    public static void isNull(Object object, ResultInfoInterface errorInfo) {

        if (Objects.isNull(object)) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    public static void isBlank(String content, String errorInfo) {
        if (StringUtils.isBlank(content)) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    public static void isBlank(String content, int code, String errorInfo) {
        if (StringUtils.isBlank(content)) {
            throw new GlobalErrorInfoException(code, errorInfo);
        }
    }

    public static void isBlank(String content, ResultInfoInterface errorInfo) {
        if (ObjectUtils.isEmpty(content)) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    /**
     * 如果array为空，则抛异常
     *
     * @param array
     * @param errorInfo
     */
    public static void isEmpty(Object[] array, ResultInfoInterface errorInfo) {
        if (ObjectUtils.isEmpty(array)) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    /**
     * 如果collection为空，则抛异常
     *
     * @param collection 集合
     * @param errorInfo
     */
    public static void isEmpty(Collection<Object> collection, ResultInfoInterface errorInfo) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    /**
     * 如果expression非true，则抛异常
     *
     * @param expression
     * @param errorInfo
     */
    public static void unTrue(boolean expression, String errorInfo) {
        if (!expression) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }

    public static void unTrue(boolean expression, int code, String errorInfo) {
        if (!expression) {
            throw new GlobalErrorInfoException(code, errorInfo);
        }
    }

    public static void unTrue(boolean expression, ResultInfoInterface errorInfo) {
        if (!expression) {
            throw new GlobalErrorInfoException(errorInfo);
        }
    }
}
